home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / objam01.lha / objam / objbas / Array.m < prev    next >
Encoding:
Text File  |  1994-12-14  |  2.5 KB  |  129 lines

  1. /*
  2. ** ObjectiveAmiga: Implementation of class Array
  3. ** See GNU:lib/libobjam/ReadMe for details
  4. */
  5.  
  6.  
  7. #include <stdarg.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #import "Array.h"
  11.  
  12.  
  13. @implementation Array
  14.  
  15. + (unsigned)ndxVarSize
  16. {
  17.   [self subclassResponsibility:@selector(ndxVarSize)];
  18.   return 0;
  19. }
  20.  
  21. + new:(unsigned)nElements { return [[self alloc] init:nElements]; }
  22.  
  23. + with:(unsigned)nArgs,...
  24. {
  25.   id newArray;
  26.   unsigned i;
  27.   va_list args;
  28.  
  29.   if(!(newArray=[self new:nArgs])) return nil;
  30.   va_start(args,nArgs);
  31.   for(i=0;i<nArgs;i++) [newArray add:va_arg(args,id)];
  32.   va_end(args);
  33.  
  34.   return newArray;
  35. }
  36.  
  37. - init { return [self init:1]; }
  38.  
  39. - init:(unsigned)nElements
  40. {
  41.   int size;
  42.  
  43.   if(!(self=[super init])) return nil;
  44.   capacity=nElements;
  45.   size=[[self class] ndxVarSize]*capacity;
  46.   if(!(elements=malloc(size))) return [self free];
  47.   bzero(elements,size);
  48.   return self;
  49. }
  50.  
  51. - free
  52. {
  53.   if(elements) { free(elements); elements=NULL; capacity=0; }
  54.   return [super free];
  55. }
  56.  
  57. - add:dummy { return [self subclassResponsibility:@selector(ndxVarSize)]; }
  58.  
  59. - (unsigned)capacity { return capacity; }
  60.  
  61. - capacity:(unsigned)nSlots
  62. {
  63.   capacity=nSlots;
  64.   if(elements=realloc(elements,[[self class] ndxVarSize]*capacity)) return self;
  65.   else return [self free];
  66. }
  67.  
  68. - (STR)describe
  69. {
  70.   [self subclassResponsibility:@selector(describe)];
  71.   return 0;
  72. }
  73.  
  74. - boundsViolation:(unsigned)anOffset
  75. {
  76.   return [self error: capacity>0 ?
  77.     "bounds violation: %d outside range [0..%d]":
  78.     "zero capacity array",
  79.     anOffset, capacity-1];
  80. }
  81.  
  82. - copy
  83. {
  84.   Array *newArray;
  85.   void *newElements;
  86.   unsigned int arraySize=[[self class] ndxVarSize]*capacity;
  87.  
  88.   if(newArray=[super copy])
  89.   {
  90.     if(newElements=malloc(arraySize))
  91.     {
  92.       memcpy(newElements,elements,arraySize);
  93.       newArray->elements=newElements;
  94.       return newArray;
  95.     }
  96.     [newArray free];
  97.   }
  98.  
  99.   return nil;
  100. }
  101.  
  102. - read:(TypedStream*)stream
  103. {
  104.   unsigned i, elementLength=[[self class] ndxVarSize];
  105.   STR types=[self describe];
  106.  
  107.   [self init];
  108.   [super read:stream];
  109.   objc_read_types(stream,"I",&capacity);
  110.   [self init:capacity];
  111.   for(i=0;i<capacity;i++) objc_read_types(stream,types,(void *)(((unsigned)(elements))+i*elementLength));
  112.  
  113.   return self;
  114. }
  115.  
  116. - write:(TypedStream*)stream
  117. {
  118.   unsigned i, elementLength=[[self class] ndxVarSize];
  119.   STR types=[self describe];
  120.  
  121.   [super write:stream];
  122.   objc_write_types(stream,"I",&capacity);
  123.   for(i=0;i<capacity;i++) objc_write_types(stream,types,(void *)(((unsigned)(elements))+i*elementLength));
  124.  
  125.   return self;
  126. }
  127.  
  128. @end
  129.